#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
 /**
  * Note: The returned array must be malloced, assume caller calls free().
  */
int TreeSize(struct TreeNode* root)
{
    return root == NULL ? 0 : TreeSize(root->left) + TreeSize(root->right) + 1;
}

void PrevOrder(struct TreeNode* root, int* arr, int* pi)
{
    if (root == NULL)
        return;
    arr[(*pi)++] = root->val;
    PrevOrder(root->left, arr, pi);
    PrevOrder(root->right, arr, pi);
}

int* preorderTraversal(struct TreeNode* root, int* returnSize) {
    *returnSize = TreeSize(root);
    int i = 0;
    int* arr = (int*)malloc(sizeof(int) * (*returnSize));
    PrevOrder(root, arr, &i);
    return arr;
}